package com.twu.biblioteca.System;

import com.twu.biblioteca.Helper.Utility;
import com.twu.biblioteca.Models.*;

import java.util.ArrayList;
import java.util.Scanner;


/**
 * Created by ubuntu on 4/28/14.
 */
public class BibliotecaSystem {

    Utility utility = new Utility();
    SystemMessages systemMessages = new SystemMessages();
    Library library;
    Data data;
    Login login;

    public BibliotecaSystem(Data data,Library library) {
        this.library = library;
        this.data=data;
        login = new Login(data);
    }

    ArrayList<Menu> menus = new ArrayList<>();
    Scanner scanner = new Scanner(System.in);

    private final String QUIT_OPTION = "Q";

    public void welcome() {
        System.out.println(systemMessages.getWelcomeMessage());
    }

    public void startSystem() {
        welcome();
        while (!login.authenticate());
        String key;
        do {
            System.out.println("Choose an Option:");
            showMainMenu();
            key = scanner.next();
            if(!key.equalsIgnoreCase(QUIT_OPTION))
                doAction(key);
        } while (!key.equalsIgnoreCase(QUIT_OPTION));
    }

    private void doAction(String key) {
        if(key.equalsIgnoreCase(SystemConfiguration.CHECKOUT_BOOK)) {
            System.out.println("Enter Name of the Book to Checkout:");
            String bookName = scanner.next();
            checkOutItemFromLibrary(bookName, Book.class);
        }
        else if(key.equalsIgnoreCase(SystemConfiguration.RETURN_BOOK)){
            System.out.println("Enter Name of the Book to Return");
            String bookName = scanner.next();
            returnItemToLibrary(bookName, Book.class);
        }
        else if(key.equalsIgnoreCase(SystemConfiguration.LIST_BOOKS))
        {
            System.out.println("Books in library:");
            listItems(Book.class);
        }
        else if(key.equalsIgnoreCase(SystemConfiguration.LIST_MOVIES)) {
            System.out.println("Movies in library:");
            listItems(Movie.class);
        }
        else if(key.equalsIgnoreCase(SystemConfiguration.RETURN_MOVIE)){
            System.out.println("Enter Name of the Movie to Return");
            String movieName = scanner.next();
            returnItemToLibrary(movieName,Movie.class);
        }
        else if(key.equalsIgnoreCase(SystemConfiguration.CHECKOUT_MOVIE)){
            System.out.println("Enter Name of the Movie to Checkout:");
            String movieName = scanner.next();
            checkOutItemFromLibrary(movieName,Movie.class);
        }
        else if(key.equalsIgnoreCase(SystemConfiguration.GET_USER_INFORMATION)){
            System.out.println("User Information:[Name, email, contact]");
            System.out.println(data.getLoggedInUser().toString());
        }
        else
            System.out.println(systemMessages.getInvalidMenuOptionMessage());
    }

    private<T extends Item> void listItems(Class<T> classType) {
        ArrayList<T> allItems = library.getAllItems(classType);
        for (T item : allItems) {
            if(item.isAvailable())
                System.out.println(item.getTitle());
        }
    }

    private <T extends Item> void returnItemToLibrary(String bookName, Class<T> classType) {
        if(library.returnBackToLibrary(bookName,classType)) {
            System.out.println(systemMessages.getSuccessfulBookReturnMessage(classType));
        }
        else
            System.out.println(systemMessages.getUnSuccessfullReturnMessage(classType));
    }

    private void showMainMenu() {
        System.out.println(utility.convertListToString(menus, "\n"));
    }

    public void addMenu(Menu menu) {
        menus.add(menu);
    }


    public <T extends Item> void checkOutItemFromLibrary(String itemName,Class<T> classType) {
        if(library.borrowItem(itemName, classType)){
            System.out.println(systemMessages.getSuccessfulBookCheckOutMessage(classType));
        }else {
            System.out.println(systemMessages.getUnSuccessfulBookCheckOutMessage(classType));
        }
    }

}
